home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / gnustuff / tos / util / utlsrc37.zoo / printstk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-30  |  4.2 KB  |  209 lines

  1. /* 
  2.  * utility to print the value of _stksize from gcc-cc1.ttp
  3.  *
  4.  *    Usage: printstk [<filename>]
  5.  *        if <filename> is not specified defaults to .\gcc-cc1.ttp
  6.  *    ++jrb
  7.  *
  8.  *      modified to print a value of _initial_stack in cases when this
  9.  *      is defined instead of _stksize -- mj
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <st-out.h>
  14.  
  15. #if __STDC__
  16. #include <stdlib.h>
  17. #include <unistd.h>
  18. #include <string.h>
  19. #else
  20. #include <string.h>
  21. extern char *malloc();
  22. extern long lseek();
  23. #define size_t unsigned long
  24. #endif
  25.  
  26. #ifndef FILENAME_MAX
  27. # define FILENAME_MAX 128
  28. #endif
  29.  
  30. #ifdef WORD_ALIGNED
  31. # define SIZEOF_AEXEC ((2*sizeof(short)) + (6*sizeof(long)))
  32. # ifndef SYMLEN
  33. #   define SYMLEN 8
  34. # endif
  35. # define SIZEOF_ASYM  ((SYMLEN*sizeof(char)) + sizeof(short) + sizeof(long))
  36. # define SYM_OFFSET   (sizeof(short) + (3*sizeof(long)))
  37. #endif
  38.  
  39. int error = 0;
  40.  
  41. static char *sym_names[] = { "__stksize", "__initial_stack" };
  42.  
  43. long find_offset (fd, fn, what)
  44. int fd;
  45. char *fn;
  46. int *what;
  47. {
  48.     struct aexec head;
  49.     struct asym  sym;
  50.     int    all = 0;
  51.     int index = 1;
  52. #ifndef WORD_ALIGNED
  53.     char extension[sizeof (struct asym)];
  54. #else
  55.     char extension[SIZEOF_ASYM];
  56. #endif
  57.     
  58. #ifndef WORD_ALIGNED
  59.     if(read(fd, &head, sizeof(head)) != sizeof(head))
  60. #else
  61.     if(read_head(fd, &head))
  62. #endif
  63.     {
  64.     perror(fn);
  65.     return -1;
  66.     }
  67.     if(head.a_magic != CMAGIC)
  68.     {
  69.     fprintf(stderr,"%s: invalid magic number %x\n", fn, head.a_magic);
  70.     return -1;
  71.     }
  72.     if(head.a_syms == 0)
  73.     {
  74.     fprintf(stderr,"%s: no symbol table\n", fn);
  75.     return -1;
  76.     }
  77.     if(lseek(fd, head.a_text+head.a_data, 1) != 
  78. #ifndef WORD_ALIGNED
  79.        (head.a_text+head.a_data+sizeof(head)))
  80. #else
  81.        (head.a_text+head.a_data+SIZEOF_AEXEC))
  82. #endif
  83.     {
  84.     perror(fn);
  85.     return -1;
  86.     }
  87.     for(;;)
  88.     {
  89. #ifndef WORD_ALIGNED
  90.     if(index && (read(fd, &sym, sizeof(sym)) != sizeof(sym)))
  91. #else
  92.     if(index && read_sym(fd, &sym))
  93. #endif
  94.     {
  95.         fprintf(stderr, "%s: symbol _stksize not found\n", fn);
  96.         return -1;
  97.     }
  98.     if (index && (sym.a_type & A_LNAM) == A_LNAM)
  99.       if (read (fd, extension, sizeof (extension)) != sizeof (extension))
  100.         {
  101.           fprintf (stderr, "%s: symbol _stksize not found\n", fn);
  102.           return -1;
  103.         }
  104.     /* after symbol read check first for _stksize */
  105.     index ^= 1;
  106.     if (strncmp(sym_names[index], sym.a_name, 8) == 0)
  107.     {
  108.       if ((sym.a_type & A_LNAM) == A_LNAM
  109.           && strncmp (sym_names[index] + 8, extension, sizeof (extension)))
  110.         continue;
  111.       if (sym.a_type & A_DATA)
  112.         break;
  113.       if (all++)
  114.         {
  115.           fprintf (stderr, "%s: symbol _stksize is undefined\n", fn);
  116.           return -1;
  117.         }
  118.     }
  119.     }
  120.     
  121.     *what = index;
  122. #ifndef WORD_ALIGNED
  123.     return sym.a_value + sizeof(head);
  124. #else
  125.     return sym.a_value + SIZEOF_AEXEC;
  126. #endif
  127. }
  128.  
  129. int main(argc, argv)
  130. int argc;
  131. char **argv;
  132. {
  133.   while (--argc)
  134.     error |= print_stack (*++argv);
  135.   exit (error);
  136. }
  137.  
  138. int
  139. print_stack (fn)
  140.      char *fn;
  141. {
  142.     int fd;
  143.     int what;
  144.     long stksize, offset;
  145.     
  146.     if((fd = open(fn, 0)) < 0)
  147.     {
  148.     perror(fn);
  149.     return 1;
  150.     }
  151.     
  152.     offset = find_offset(fd, fn, &what);
  153.     if (offset < 0)
  154.       {
  155.     close (fd);
  156.     return 1;
  157.       }
  158.  
  159.     if(lseek(fd, offset, 0) != offset)
  160.     {
  161.     perror(fn);
  162.     close (fd);
  163.     return 1;
  164.     }
  165.     read(fd, &stksize, sizeof(long));
  166.     printf("%s: %s is %ld (%dK)\n",
  167.          fn, sym_names[what] + 1, stksize, (int)(stksize/1024));
  168.     
  169.     return close(fd) != 0;
  170. }
  171.  
  172. #ifdef WORD_ALIGNED
  173. #ifndef atarist
  174. # define lread  read
  175. # define lwrite write
  176. #endif
  177.  
  178. /*
  179.  * read header -- return !0 on err
  180.   */
  181. #define ck_read(fd, addr, siz) \
  182.   if((long)siz != lread(fd, addr, (long)siz)) return !0;
  183.   
  184. int read_head (fd, a)
  185. int fd;
  186. struct aexec *a;
  187. {
  188.     ck_read(fd, &a->a_magic,   sizeof(a->a_magic));
  189.     ck_read(fd, &a->a_text,    sizeof(a->a_text));
  190.     ck_read(fd, &a->a_data,    sizeof(a->a_data));
  191.     ck_read(fd, &a->a_bss,     sizeof(a->a_bss));
  192.     ck_read(fd, &a->a_syms,    sizeof(a->a_syms));
  193.     ck_read(fd, &a->a_AZero1,  sizeof(a->a_AZero1));
  194.     ck_read(fd, &a->a_AZero2,  sizeof(a->a_AZero2));
  195.     ck_read(fd, &a->a_isreloc, sizeof(a->a_isreloc));
  196.     return 0;
  197. }
  198.  
  199. int read_sym(fd, s)
  200. int fd;
  201. struct asym *s;
  202. {
  203.     ck_read(fd, s->a_name, 8);
  204.     ck_read(fd, &(s->a_type), sizeof(short));
  205.     ck_read(fd, &(s->a_value), sizeof(long));
  206.     return 0;
  207. }
  208. #endif
  209.